package com.jade.gateway.controller;

import com.jade.gateway.facade.GatewayFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.route.RouteDefinition;
import org.springframework.context.annotation.Lazy;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * GatewayController 网关控制器
 * @author Arnold
 * @version 2021/12/23 17:11
 **/
@RestController
@RequestMapping("gateway")
@Validated
public class GatewayController {

    @Autowired
    @Lazy
    GatewayFacade gatewayFacade;

    /**
      * 新增路由
      * @param routerDefinitions 路由参数集合
      * @return java.lang.Boolean        
      * @author Arnold
      * @date 2022/7/1
      *
     **/
    @PostMapping("/route/add")
    public Boolean addRoute(@RequestBody @NotEmpty(message = "路由参数集合 is Empty") List<RouteDefinition> routerDefinitions) {
        return gatewayFacade.addRoute(routerDefinitions);
    }

    /**
      * 删除路由
      * @param id 路由id
      * @return java.lang.Boolean
      * @author Arnold
      * @date 2022/7/1
      *
     **/
    @DeleteMapping("/route/delete/{id}")
    public Boolean deleteRoute(@PathVariable("id") @NotBlank(message = "路由id is Blank") String id) {
        return gatewayFacade.deleteRoute(id);
    }

    /**
      * 删除路由
      * @param routerDefinition 路由参数
      * @return java.lang.Boolean
      * @author Arnold
      * @date 2022/7/1
      *
     **/
    @PutMapping("/route/update")
    public Boolean updateRoute(@RequestBody @NotNull(message = "路由参数 is null") RouteDefinition routerDefinition) {
        return gatewayFacade.updateRoute(routerDefinition);
    }

}
